package com.aplink.generic.xmlparser;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.text.TextUtils;

import com.aplink.generic.util.ResourcesHelper;
import com.aplink.generic.util.validation.BlankValidation;
import com.aplink.generic.util.validation.DuplicateValidation;
import com.aplink.generic.util.validation.LocationValidation;
import com.aplink.generic.util.validation.Validation;

public class ValidationParser {
    public static final String ACCURACY = "accuracy";
    public static final String DISTANCE = "distance";
    public static final String MESSAGE = "message";
    public static final String NO_BLANK = "noblank";
    public static final String NO_DUPLICATE = "noduplicate";
    public static final String VALID_LOCATION = "validlocation";

    private static BlankValidation parseBlankRule(final Element elementFormat) {
        final NodeList nodeListFormat = elementFormat
                .getElementsByTagName(ValidationParser.NO_BLANK);
        Node node = null;
        String value = null;
        if (nodeListFormat.getLength() == 1) {
            node = nodeListFormat.item(0);
            final BlankValidation duplicate = new BlankValidation();
            if (node.hasAttributes()) {
                value = ResourcesHelper.getStringByName(XMLPaserUtil
                        .getAttrValue(node, ValidationParser.MESSAGE));
                duplicate.setMessage(value);
            }
            return duplicate;
        }
        return null;
    }

    private static DuplicateValidation parseDuplicateRule(
            final Element elementFormat) {
        final NodeList nodeListFormat = elementFormat
                .getElementsByTagName(ValidationParser.NO_DUPLICATE);
        Node node = null;
        String value = null;
        if (nodeListFormat.getLength() == 1) {
            node = nodeListFormat.item(0);
            final DuplicateValidation duplicate = new DuplicateValidation();
            if (node.hasAttributes()) {
                value = ResourcesHelper.getStringByName(XMLPaserUtil
                        .getAttrValue(node, ValidationParser.MESSAGE));
                duplicate.setMessage(value);
            }
            return duplicate;
        }
        return null;
    }

    private static LocationValidation parseValidLocation(
            final Element elementFormat) {
        final NodeList nodeListFormat = elementFormat
                .getElementsByTagName(ValidationParser.VALID_LOCATION);
        Node node = null;
        String value = null;
        if (nodeListFormat.getLength() == 1) {
            node = nodeListFormat.item(0);
            final LocationValidation duplicate = new LocationValidation();
            if (node.hasAttributes()) {
                value = ResourcesHelper.getStringByName(XMLPaserUtil
                        .getAttrValue(node, ValidationParser.MESSAGE));
                if (!TextUtils.isEmpty(value)) {
                    duplicate.setMessage(value);
                }

                value = ResourcesHelper.getStringByName(XMLPaserUtil
                        .getAttrValue(node, ValidationParser.ACCURACY));
                if (!TextUtils.isEmpty(value)) {
                    duplicate.setAccuracy(Float.parseFloat(value));
                } else {
                    duplicate.setAccuracy(10.0F);
                }

                value = ResourcesHelper.getStringByName(XMLPaserUtil
                        .getAttrValue(node, ValidationParser.DISTANCE));
                if (!TextUtils.isEmpty(value)) {
                    duplicate.setDistance(Float.parseFloat(value));
                } else {
                    duplicate.setDistance(30.0F);
                }
            }
            return duplicate;
        }
        return null;
    }

    public static Validation validationParser(final Node nodeFormat) {
        final Validation validation = new Validation();
        final Element elementFormat = (Element) nodeFormat;
        validation.setDuplicate(ValidationParser
                .parseDuplicateRule(elementFormat));
        validation.setBlankRule(ValidationParser.parseBlankRule(elementFormat));
        validation.setLocationValidation(ValidationParser
                .parseValidLocation(elementFormat));
        return validation;
    }
}
